/* Copyright (c) 2016-2020 Enfry Ltd. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Enfry. 
 * You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with Enfry.
 */
package com.enfry.common.util.config;

import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <请简单说明此类用途>
 * @Title com.enfry.util.config.Config
 * @author longguangxing
 * @date 2017年8月22日
 */
public class Config {
	public static final String EVN_TYPE = LoadProperties.getSystemdefaultValue("common.environment","/properties/sys_config.properties");
	private static Map<ConfigType, Properties> propsMap = new HashMap<ConfigType, Properties>();
	public static Logger logger = LoggerFactory.getLogger(Config.class);
	static {
		logger.info("加载的EVN_TYPE={}",EVN_TYPE);
		for(ConfigType config:ConfigType.values()){
			String properFile = String.format(config.getConfigFileName(), EVN_TYPE);
			boolean isOK = addProperties(config, properFile);
			if(!isOK){
				logger.error("{}文件properFile={}可能不存在，又或者不需要加载该文件",config.getZhName(),properFile);
			}
		}
	}
	
	public static Map<String, Object> getProperties(ConfigType config, String propFileName) {
		Map<String, Object> ctxPropertiesMap = new HashMap<String, Object>();
		try {
			URL url = ClassLoaderUtils.getResource(propFileName, Config.class);
			if(url!=null){
				Properties props = LoadProperties.load(url);
				for (Object key : props.keySet()) {  
		            String keyStr = key.toString();  
		            String value = props.getProperty(keyStr);  
		            ctxPropertiesMap.put(keyStr, value);  
		        }  
			}
		} catch (Exception e) {
			logger.error("{}文件properFile={}可能不存在，又或者不需要加载该文件",config.getZhName(),propFileName);
		}
		return ctxPropertiesMap;
	}
	
	private static boolean addProperties(ConfigType config, String propFileName) {
		boolean isloadOK = false;
		try {
			URL url = ClassLoaderUtils.getResource(propFileName, Config.class);
			if(url!=null){
				Properties props = LoadProperties.load(url);
				propsMap.put(config, props);
				isloadOK = true;
			}
		} catch (Exception e) {
			isloadOK = false;
		}
		return isloadOK;
	}
	
	public static String getString(ConfigType config, String propKey){
		Properties props = propsMap.get(config);
		if (props != null) {
			return props.getProperty(propKey);
		}
		return null;
	}
	
	public static boolean getBoolean(ConfigType config, String propKey){
		Properties props = propsMap.get(config);
		if (props != null) {
			String property = props.getProperty(propKey);
			return Boolean.parseBoolean(property);
		}
		return false;
	}
	
	public static Integer getInteger(ConfigType config, String propKey){
		String propValue = getString(config, propKey);
		if (propValue != null) {
			return Integer.parseInt(propValue);
		}
		return null;
	}
	
	public static String getKeyByValue(ConfigType config, String propValue){
		String keys = "";
		Properties props = propsMap.get(config);
		if (props != null) {
			for(Object key:props.keySet()){
				Object value = props.get(key);
				if(value!=null&&value.equals(propValue)){
					keys = (String) key;
				}
			}
		}
		return keys;
	}
}
